
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.query;

import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import utility.CapitalChar;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Operation implements Queryinterface
{	

	String object;
	String buffer[];
	Vector<Object> operation = new Vector<Object>();
	String OPERATION="operation";
	GetIdName gid;
	String TYPE = "type";
	String Field;
	String heading;
	String field;
	String COLUMN = "column";
	String Heading;
	String reportname;
	Hashtable<Object,Object> ReportMap =new Hashtable<Object, Object>();
	String typeofreport;
	String Key;
	String Value;
	String reporton;
	
	PoolLogger pl;
	
	
	public void queryProcess(){
		getResult();

	}
	
	public Operation() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public void setObject(String object) 
	{
		this.object = object;
	}
	
	public String[] executeQuery(){return null;}
	
	
	
	public String[] getBuffer() 
	{
		return buffer;
	}
	@SuppressWarnings("unchecked")
	public void getResult() 
	{	
		RecordSearch rs = new RecordSearch();
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		table.put("mid", object);
		table.put("pid", OPERATION);
		table.put("td", "null");
		rs.setConditionMap(table);
		
		String line[] = rs.getArrayFromResultSet();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value = splitline[Column.pv_index-1];
			if( value.equals("0") || value.equals("no$") )
			{
				value = splitline[Column.vt_index-1]; 
			}
			operation.add(value);
		}
		table.put("pid", COLUMN);
		COLUMN = "no$";
		rs.setConditionMap(table);
		line = rs.getArrayFromResultSet();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value = splitline[Column.pv_index-1];
			if( value.equals("0") || value.equals("no$") )
				value = splitline[Column.vt_index-1]; 
			else
				value = gid.getItem(value);
		}
		for(int i=0;i<operation.size();i++)
		{ 
			table.clear();
			table.put("mid",operation.get(i).toString());
			table.put("pid", TYPE);
			rs.setConditionMap(table);
			line = rs.getArrayFromResultSet();
			for(int j=0;j<line.length;j++)
			{
				String splitline[] = line[j].split("\t");
				String classname = splitline[Column.pv_index-1];
				if(classname.equals("0") || classname.equals("no$"))
					classname  = splitline[Column.vt_index-1];
				else	
					classname = gid.getItem(classname);
				query.reportoperations.Operation operationobject ;
				try 
				{	
					Class C = Class.forName("query.reportoperations."+CapitalChar.makeFirstCharCapital(classname));
					operationobject = (query.reportoperations.Operation) C.newInstance();
					operationobject.setbuffer(buffer);
					operationobject.initPool();
					operationobject.setObject(operation.get(i).toString());
					operationobject.setHeading(buffer[0]);	
					operationobject.setreporton(reporton);
					operationobject.getresult();
					field= operationobject.getfield();
					buffer = (operationobject.getbuffer());
					Heading = operationobject.getHeading();
					StringBuffer strbuffer =new StringBuffer(Heading+"\n" );
					for(int k=0;k<buffer.length;k++)
					{
						strbuffer.append(buffer[k]+"\n");
					}
					
					buffer = strbuffer.toString().split("\n");
				}
				catch (ClassNotFoundException e) 
				{
					e.printStackTrace();
				} 
				catch (InstantiationException e) 
				{
					e.printStackTrace();
				}
				
				catch (IllegalAccessException e) 
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public void setBuffer(String[] Buffer)  
	{
		this.buffer = Buffer;
	}

	public void setField(String Field) 
	{
		this.Field = Field; 
	}
	public void setoperationindex(int index) 
	{
		
	}
	public String getheading() 
	{
		return Heading;
	}
	public void setheading(String heading) 
	{
		this.heading =  heading;
	}
	public String getField() 
	{
			return field;
	}
	public void addReport() 
	{	
		/*		try 
		{
			Typereport typereport;
			Class C = Class.forName("query.condition.typereport."+CapitalChar.makeFirstCharCapital(typeofreport));
			typereport = (Typereport) C.newInstance();
			typereport.setData(Key, Value, database, ReportMap);
			String path=typereport.getPath(reportname);
			StringBuffer datafromfile = new StringBuffer();
			java.io.File file =new java.io.File(path); 
			RandomAccessFile raf =new RandomAccessFile(file,"r");
			String line;
			while((line=raf.readLine())!=null)
			{
				datafromfile.append(line+"\n");
			}
			file.deleteOnExit();
			for(int i=0;i<buffer.length;i++)
			{
				datafromfile.append(buffer[i]+"\t");
			}
			buffer = datafromfile.toString().split("\n");
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (InstantiationException e) 
		{	
			e.printStackTrace();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}*/
		getResult();		
	}
	public void updateReport() 
	{
		getResult();
	}
	public void setReportMap(Hashtable<Object, Object> ReportMap) 
	{
	 this.ReportMap = ReportMap;	
	}
	public void setReportName(String ReportName) 
	{
	 this.reportname = ReportName;	
	}
	public void settypeofreport(String typeofreport) 
	{
	 this.typeofreport = typeofreport;
	}
	public void setKey(String key) 
	{
	 this.Key = key;	
	}
	public void setValue(String Value) 
	{
	 this.Value = Value;	
	}
	public void setreporton(String reporton) 
	{
	 this.reporton = reporton;	
	}
	public String getQuery() 
	{
		return null;
	}
	public Hashtable<Object, Object> getQueryMap() 
	{
		return null;
	}
	public String[] getQuerybuffer() 
	{
		return null;
	}
	public String getReportname() 
	{
		return null;
	}
	
	public String[] getbuffer() 
	{
		return buffer;
	}
	
	public int getcount() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public String getkey() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String gettablename() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String gettid() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getvalue() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void initializeData() {
		// TODO Auto-generated method stub
		
	}
	
	public void setCount(int count) {
		// TODO Auto-generated method stub
		
	}
	
	public void initPool()
	{
		gid= new GetIdName();
		TYPE =  gid.getId(TYPE);
		OPERATION = gid.getId(OPERATION);
		COLUMN = gid.getId(COLUMN);
	}
	
	public void setMaxcount(int count) {
		// TODO Auto-generated method stub
		
	}
	
	
	public void setQuery(String QueryName) {
		// TODO Auto-generated method stub
		
	}
	
	public void setQueryMap(Hashtable<Object, Object> QueryMap) {
		// TODO Auto-generated method stub
		
	}
	
	public void setQuerybuffer(String[] Querybuffer) {
		// TODO Auto-generated method stub
		
	}
	
	public void setReportmap(Hashtable<Object, Object> ReportMap) {
		// TODO Auto-generated method stub
		
	}
	
	public void setViewObject(String object, char ch) {
		// TODO Auto-generated method stub
		
	}
	
	public void setViewObject(Vector<Object> PropertyMap) {
		// TODO Auto-generated method stub
		
	}
	
	public void setbuffer(String[] Querybuffer)
	{
		this.buffer =Querybuffer;
	}
	
	public void settablename(String tablename) {
		// TODO Auto-generated method stub
		
	}
	
	public void settid(String tid) {
		// TODO Auto-generated method stub
		
	}
	
	public void buildQuery() {
		// TODO Auto-generated method stub
		
	}
	public void setConditionMap(Vector<Object> QueryMap)
	{
		
	}
	public void setProcess(String process) {
		// TODO Auto-generated method stub
		
	}
}
